import {useContractRead, useContractWrite} from "wagmi";
import firstDAOAbi from "@/abi/FirstDAO.abi";
import firstDAOTempleAbi from "@/abi/FirstDAOTemple.abi";
import {message} from "antd";
import {readContract, writeContract} from "@wagmi/core";
import NFTconfig from "@/config/NFTconfig";

export const ZeroAddress = '0x0000000000000000000000000000000000000000';
export const commonError = (error, variables, context) => {
    console.log('err', error.name)
    // console.log('var', variables)
    // console.log('contex', context)
    // if(error)
    if (error.name === 'ConnectorNotFoundError')
        message.error('请链接钱包')
    else message.error('未知错误')
}

export function useFirstDAORead(name: string, options?: { [key: string]: any }) {
    return useContractRead({
        address: NFTconfig.FirstDAO_address,
        abi: firstDAOAbi,
        // functionName: 'maxSupply',
        functionName: name,
        onError: commonError,
        ...(options || {}),
    } as any);
}

export function useTempleRead(name: string, options?: { [key: string]: any }) {
    return useContractRead({
        address: NFTconfig.Temple_address,
        abi: firstDAOTempleAbi,
        // functionName: 'maxSupply',
        functionName: name,
        onError: commonError,
        ...(options || {}),
    } as any);
}

export function readFirstDAO(name: string, options?: { [key: string]: any }) {
    return readContract({
        address: NFTconfig.FirstDAO_address,
        abi: firstDAOAbi,
        functionName: name,
        ...(options || {})
    } as any);
}

export function readTemple(name: string, options?: { [key: string]: any }) {
    return readContract({
        address: NFTconfig.Temple_address,
        abi: firstDAOTempleAbi,
        // functionName: 'maxSupply',
        functionName: name,
        ...(options || {}),
    } as any);
}

export function useFirstDAOWrite(name: string, options?: { [key: string]: any }) {
    return useContractWrite({
        address: NFTconfig.FirstDAO_address,
        abi: firstDAOAbi,
        // functionName: 'maxSupply',
        functionName: name,
        onError: commonError,
        ...(options || {}),
    } as any);
}

export function writeFirstDAO(name: string, options?: { [key: string]: any }) {
    return writeContract({
        address: NFTconfig.FirstDAO_address,
        abi: firstDAOAbi,
        functionName: name,
        ...(options || {})
    } as any);
}

export function useTempleWrite(name: string, options?: { [key: string]: any }) {
    return useContractWrite(
        {
            address: NFTconfig.Temple_address,
            abi: firstDAOTempleAbi,
            functionName: name,
            onError: commonError,
            ...(options || {}),
        } as any);
}

export function writeTemple(name: string, options?: { [key: string]: any }) {
    return writeContract({
        address: NFTconfig.Temple_address,
        abi: firstDAOTempleAbi,
        functionName: name,
        ...(options || {})
    } as any);
}

export function isEthAddress(str: string): boolean {
    if (str.length !== 42 || !str.startsWith("0x")) {
        return false;
    }
    const lowerCaseStr = str.toLowerCase();
    const hexRegExp = /^[0-9a-fA-F]+$/;
    return hexRegExp.test(lowerCaseStr.slice(2));
}
